<%-- 
    Document   : likecontent
    Created on : Apr 25, 2012, 2:49:19 PM
    Author     : Dell
--%>
<%@ page language="java" import="java.sql.*" %>
<%@ page import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<%@ page import="java.util.List"%>  
<%@ page import="java.util.Iterator"%>  
<%@ page import="org.apache.commons.fileupload.*"%>  
<%@ page import="org.apache.commons.fileupload.disk.*"%>  
<%@ page import="org.apache.commons.fileupload.servlet.*"%> 
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>

<%
    String u = request.getParameter("u");
    String c = request.getParameter("c");
    
    String driver = "org.gjt.mm.mysql.Driver";
    Class.forName(driver).newInstance();

    Connection con=null;
    ResultSet rst=null;
    ResultSet rsx=null;
    Statement stmt=null;

    try{
            String url="jdbc:mysql://localhost:3306/progin_171_13509008?user=root";
            con=DriverManager.getConnection(url);
            stmt=con.createStatement();
    }
    catch(Exception e){
            System.out.println(e.getMessage());
    }
    
    String query = "select status from like_content where user_name ='"+u+"' and id_content ="+c;
    rst=stmt.executeQuery(query);
    
    String insquery = "";
    Integer incase = 0;
    Integer sum = 0;
    while(rst.next())
               {
                sum++;
       }
    rst.first();
    if(session.getAttribute("session").equals("false")){
 
            //kalau belum pernah dimasukin
             if(sum == 0){
                 insquery = "insert into like_content (user_name,id_content,status) VALUES ('"+u+"',"+c+",1)";
                 incase = 0;
                             }
             
             else {
                   if(rst.getInt("status") == 1){insquery="update like_content set status = 0 where user_name='"+u+"' and id_content="+c+" "; incase = 1;
                   
                   }
                                     else{insquery="update like_content set status = 1 where user_name='"+u+"' and id_content="+c+" "; incase = 2;
                                     }
                   
                   stmt.executeUpdate(insquery);
                            }
    }
    out.print(incase);
%>